<?php
class Articles_modul  extends CI_Model {

	public $rub = array();
	var $one = 1 ;
	
	
	public function __construct() {
		// Call the Model constructor
		parent::__construct();

	} 
	public function listing($start = 0,$finish = 10) {
	 // запрос списка новостей 
 
	 	 $select ="SELECT
				rubric.id rubric_id,
				rubric.name rubric_name,
				rubric.url rubric_url,
				news.id news_id, 
				news.name news_name,
				news.time news_time,
				news.text news_text,
				news.img news_img, 
				u.* 
				 FROM news join rubric on news.rubric 	= rubric.id
				 left join admin_users u on news.users 	= u.id
				 where news.activ = '1'   and  rubric.pub = 1 
				 and  news.delete = '0' group by news.id desc limit {$start}, {$finish}"; 
				$q = $this ->db-> query($select); 
		if ($q -> num_rows() > 0) {
			foreach ($q -> result() as $row) {
				$news[] = array(
				'rubric_id' => $row -> rubric_id, 
				'rubric_name' => $row -> rubric_name, 
				'rubric_url' => $row -> rubric_url, 
				'id' => $row -> news_id, 
				'news_name' => $row -> news_name,   
				'news_time'=> date("d.m.Y",$row -> news_time),  
				'news_text' => $row -> news_text,  
				'news_img' => $row -> news_img,  
				'title' => preg_replace('/#/','',$row -> news_name),  
				);
			}
		}
		
		return $news; 
	
	}
	
	
 
	public function total_news() {

		$select = "SELECT  
				count(news.id) total 
				 FROM news join rubric on news.rubric 	= rubric.id
				 left join admin_users u on news.users 	= u.id
				 where news.activ = '1'   and  rubric.pub = 1 
				 and  news.delete = '0';"; 
		$query = $this -> db -> query($select); 
		if ($query -> num_rows() > 0) {
			$row = $query -> row();
			return $row -> total;
		}
		return false;
	}
 
	 
	
	
    	public function newsid($id = 0) {
		$newsid = false;
		# Запрос всех рубрик с названиями
		$select = "SELECT * FROM `articles`  a where a.id = '$id'  and  a.activ = '1'   limit 1;";
		$query = $this -> db -> query($select);

		if ($query -> num_rows() > 0) {
			$row = $query -> row();
			$newsid = array(
			'id' => $row -> id, 
			'title' => $row -> 	title,
			'keywords' => $row -> 	keywords,
			'description' => $row -> description,
			'blockquote' => $row -> 	blockquote,
			'text' => $row -> 	text,
			'autor' => $row -> autor,
			'admincoment' => $row -> 	admincoment,
			'url' => $row -> 	url,
			'section' => $row -> 	section,
			'color' => $row -> 	color,
						  
			); 
			return $newsid;
		}
		return false;
	}

	
	   public function news($id=1) {
     // запрос только одной новости
 
         $select ="SELECT
                rubric.id rubric_id,
                rubric.name rubric_name,
                rubric.url rubric_url,
                news.id news_id, 
                news.name news_name,
                news.time news_time,
                news.text news_text,
                news.img news_img, 
                u.name user_name, 
                u.surname user_surname
                 FROM news join rubric on news.rubric   = rubric.id
                 left join admin_users u on news.users    = u.id
                 where news.activ = '1'   and  rubric.pub = 1 
                 and  news.delete = '0' and news.id = '".$id."' group by news.id desc limit 1;"; 
                 
                 
                $q = $this ->db-> query($select); 
        if ($q -> num_rows() > 0) {
            foreach ($q -> result() as $row) {
                $news[] = array(
                'rubric_id' => $row -> rubric_id, 
                'rubric_name' => $row -> rubric_name, 
                'rubric_url' => $row -> rubric_url, 
                'id' => $row -> news_id, 
                'news_name' => $row -> news_name,   
                'time'=> date("d.m.Y",$row -> news_time),  
                'text' => $row -> news_text,  
                'img' => $row -> news_img,  
                'title' => preg_replace('/#/','',$row -> news_name), 
                'user_name' => $row -> user_name,  
                'user_surname' => $row -> user_surname,  
             
             
              
                );
            }
             return $news['0']; 
        } 
        return FALSE; 
    
    }
}
